package com.youens.navigationdrawertest.ui.wrongword

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModelProviders
import com.youens.navigationdrawertest.MainActivity
import com.youens.navigationdrawertest.R
import com.youens.navigationdrawertest.database.DataBaseUtils
import com.youens.navigationdrawertest.ui.review.ReviewFragment


class WrongWordFragment : Fragment() {

    private lateinit var wrongWordViewModel: WrongWordViewModel
    lateinit var databaseUtil: DataBaseUtils
    lateinit var wrongWordListView: ListView
    lateinit var allWrongWords:MutableList<WrongWord>
    lateinit var wrongWordTestButton:Button
    lateinit var adapter:WrongWordAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        databaseUtil = (activity as MainActivity).getDataBaseUtils()
    }


    override fun onStart() {
        super.onStart()
        wrongWordListView = view!!.findViewById(R.id.wrong_word_records_listView)
        wrongWordTestButton = view!!.findViewById(R.id.btn_test_wrong_word)
        allWrongWords = databaseUtil.getAllWrongWords()
        adapter=WrongWordAdapter(allWrongWords,context!!)
        wrongWordListView.adapter = adapter

        wrongWordListView.setOnItemLongClickListener { parent, view, position, id ->
            var builder = AlertDialog.Builder(context!!)
            builder.setTitle("从错词本中移除")
            builder.setMessage("是否确认?")
            builder.setPositiveButton("是") {
                    dialog, id ->
                Toast.makeText(activity, "已移除", Toast.LENGTH_SHORT).show()
                var id = allWrongWords[position].id
                databaseUtil.deleteWrongWord(id)
                allWrongWords.removeAt(position)
                adapter.notifyDataSetChanged()
            }
            builder.setNegativeButton("否") {
                    dialog, id ->
                Toast.makeText(activity, "取消", Toast.LENGTH_SHORT).show()
            }
            val dialog: AlertDialog = builder.create()
            dialog.setCancelable(false)
            dialog.show()
            true
        }

        wrongWordTestButton.setOnClickListener {
            val fragment2 = ReviewFragment()
            val bundle = Bundle()
            // 传递字符串
            bundle.putBoolean("flag",true)
            fragment2.arguments = bundle
            //调用上面的方法由 fragment1 跳转到 fragment2
            showFragment(this, fragment2)
        }

    }

    override fun onResume() {
        super.onResume()
        //adapter.notifyDataSetChanged()
    }
    private fun showFragment(
        fragment1: Fragment,
        fragment2: Fragment
    ) {
        // 获取 FragmentTransaction  对象
        val transaction: FragmentTransaction = fragmentManager!!.beginTransaction()
        //如果fragment2没有被添加过，就添加它替换当前的fragment1
        if (!fragment2.isAdded) {
            transaction.replace(R.id.nav_host_fragment, fragment2)
                .hide(fragment1)
                .addToBackStack(null) //加入返回栈，这样你点击返回键的时候就会回退到fragment1了
                .commitAllowingStateLoss()// 提交事务
        } else { //如果已经添加过了的话就隐藏fragment1，显示fragment2
            transaction // 隐藏fragment1，即当前碎片
                .hide(fragment1) // 显示已经添加过的碎片，即fragment2
                .show(fragment2) // 加入返回栈
                .addToBackStack(null) // 提交事务
                .commitAllowingStateLoss()
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        wrongWordViewModel =
            ViewModelProviders.of(this).get(WrongWordViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_wrong_word, container, false)
        //val textView: TextView = root.findViewById(R.id.text_slideshow)
        //wrongWordViewModel.text.observe(this, Observer {
        //    textView.text = it
        //})
        return root
    }
}